﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EM.BasePage
{
    public class BaseView
    {
        public string RenderView(IBasePage basePage, string pageView)
        {
            StringBuilder view = new StringBuilder(Utils.ReadView("MainPage.html"));
            view.Replace("$PageHeader$", RenderHeader(basePage));
            view.Replace("$PageSideMenu$", RenderSideMenu(basePage));
            view.Replace("$PageTopbar$", RenderTopbar(basePage));
            view.Replace("$PageContent$", pageView);
            return view.ToString();
        }
        public static void BindView(ref StringBuilder view, BaseModel model)
        {
            System.Reflection.PropertyInfo[] properties = model.GetType().GetProperties();
            foreach (var property in properties)
            {
                view.Replace("$" + property.Name.ToString() + "$", model.GetType().GetProperty(property.Name).GetValue(model, null).ToString());
            }
        }

        public static void UnbindView(System.Collections.Specialized.NameValueCollection view, BaseModel model)
        {
            System.Reflection.PropertyInfo[] properties = model.GetType().GetProperties();
            foreach (var property in properties)
            {
                model.GetType().GetProperty(property.Name).SetValue(model, view[property.Name], null);
            }
        }

        private string RenderHeader(IBasePage basePage)
        {
            StringBuilder view = new StringBuilder(Utils.ReadView("MainPage.Header.html"));
            return view.ToString();
        }

        private string RenderSideMenu(IBasePage basePage)
        {
            List<SideMenu> SideMenus = basePage.GetPageSideMenu();
            if (SideMenus != null)
            {

                StringBuilder view = new StringBuilder(Utils.ReadView("MainPage.SideMenu.html"));
                StringBuilder sideMenuData = new StringBuilder();
                bool IsAccordianSet = false;
                bool IsAccordianChildSet = false;
                foreach (SideMenu sideMenu in SideMenus)
                {
                    if (sideMenu.IsAccordian) 
                    {
                        IsAccordianSet = true;
                        if (!sideMenu.IsAccordianChild)
                        {
                            if (IsAccordianChildSet)
                            {
                                sideMenuData.Append("</ul>");
                                IsAccordianChildSet = false;
                            }
                            sideMenuData.AppendFormat("<li><a href='#' class='accordion-toggle'><span class='{0}'></span><span class='sidebar-title'>{1}</span><span class='caret'></span></a>", sideMenu.Icon, sideMenu.Title);
                            sideMenuData.Append("<ul class='nav sub-nav'>");
                        }
                        else
                        {
                            IsAccordianChildSet = true;
                            sideMenuData.AppendFormat("<li><a href='{0}'><span class='{1}'></span><span class='sidebar-title'>{2}</span></a></li>", sideMenu.Url, sideMenu.Icon, sideMenu.Title);
                        }
                    }
                    else
                    {
                        if (IsAccordianSet)
                        {
                            sideMenuData.Append("</ul>");
                            IsAccordianSet = false;
                        }
                        sideMenuData.AppendFormat("<li><a href='{0}'><span class='{1}'></span><span class='sidebar-title'>{2}</span></a></li>", sideMenu.Url, sideMenu.Icon, sideMenu.Title);
                    }
                }
                view.Replace("$SideMenuContent$", sideMenuData.ToString());
                return view.ToString();
            }
            else
            {
                return "";
            }
        }

        private string RenderTopbar(IBasePage basePage)
        {
            StringBuilder view = new StringBuilder(Utils.ReadView("MainPage.Topbar.html"));
            return view.ToString();
        }
    }
}
